﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class admin_admin_property_images_upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string hash = WebhelperMethods.URLHash(Request);
            if (string.IsNullOrWhiteSpace(hash))
            {
                Response.Redirect("login.aspx");
                return;
            }

            PomplingCustomers customer = PomplingCustomers.CustomerSelect(hash);
            if (string.IsNullOrWhiteSpace(customer.PomplingCustomerID))
            {
                Response.Redirect("login.aspx");
                return;
            }

            string sAddressId = WebhelperMethods.GetUrlParamValue(Globals.URL_VAR_ADDRESS_ID, Request);
            if (string.IsNullOrWhiteSpace(sAddressId))
            {
                Response.Redirect("admin_save_address.aspx?c=" + Request["c"] + "&a=");
                return;
            }

            int addressID = 0;
            if (Int32.TryParse(sAddressId, out addressID))
            {
                List<PropertyImage> images = PropertyImage.SelectAdditionalImages(addressID);
                if (images.Count > 1)
                {
                    int imageDivHeight = 150;
                    int imagesInOneRow = 4;
                    int labelHeight = 20;
                    int borderStartHeight = 600;
                    int borderStyleHeight = imageDivHeight * (images.Count % imagesInOneRow);
                    borderStyleHeight = borderStartHeight + ((labelHeight * images.Count) + borderStyleHeight);
                    theForm.Style.Add("height", borderStyleHeight.ToString() + "px");

                    if (images.Count >= 8)
                    {
                        // Add a maximum number of images you can upload 
                    }
                }
                string imageHtml = string.Empty;

                for (int i = 0; i < images.Count; i++)             
                    imageHtml += "<img src=\"../" + images[i].DisplayPicturePath + "\" title=\"" + images[i].PictureTitle + "\" alt=\"" + images[i].PictureTitle + "\" " +
                        " height=\"150px\" width=\"150px\" class=\"propertyImageList\" /> ";
                
                divImageList.InnerHtml = imageHtml;
            }
        }
    }

    protected void BtnSaveImage_Click(object sender, EventArgs e)
    {
        if (TxtImageTitle.Text.Trim().Length == 0)
        {
            LblTitleError.Visible = true;
            return;
        }
        string sAddressId = WebhelperMethods.GetUrlParamValue("a", Request);
        int addressID = 0;
        Int32.TryParse(sAddressId, out addressID);          

        if (FileUpload1.HasFile)
        {
            try
            {
                string customerID = WebhelperMethods.GetUrlParamValue("c", Request);
                //string pictureTitle = 
                string folderNameWeb = PropertyImage.ImageSaveWebPath();

                string dirPath = Server.MapPath("..") + "\\" + PropertyImage.ImageSaveFilePath();
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);

                string fileName = WebhelperMethods.SecondsSinceUnixTime().ToString() + FileUpload1.FileName;
                string filePath = dirPath + "\\" + fileName;
                FileUpload1.SaveAs(filePath);
                string webImagePath = folderNameWeb + "/" + fileName;

                PropertyImage pi = new PropertyImage(customerID, addressID, false, webImagePath);
              
                pi.PictureTitle = (TxtImageTitle.Text.Trim().Length == 0) ? "No Title Given" : TxtImageTitle.Text.Trim();
                pi.SaveImagePath("INSERT");
                string newUri = "redirect.aspx" + Request.Url.Query + "&pagefrom=admin_property_images_upload";                
                Response.Redirect(newUri);
                LblMessage.Text = String.Empty;
            }
            catch (Exception)
            {
                //LblMessage.Text = "ERROR: " + ex.Message.ToString();
            }
        }
        else
            LblMessage.Text = "You have not specified a file.";
    }
    protected void BtnPrevious_Click(object sender, EventArgs e)
    {
        string newUri = "admin_property_image_main.aspx" + Request.Url.Query;
        Response.Redirect(newUri);
    }
    protected void BtnDeleteAllImages_Click(object sender, EventArgs e)
    {
        string sAddressId = WebhelperMethods.GetUrlParamValue("a", Request);
        string customerID = WebhelperMethods.GetUrlParamValue("c", Request);
        int addressID = 0;
        Int32.TryParse(sAddressId, out addressID);
        PropertyImage pi = new PropertyImage(customerID, addressID, false, string.Empty);
        pi.DeleteImagePath();
        string newUri = "redirect.aspx" + Request.Url.Query + "&pagefrom=admin_property_images_upload";
        Response.Redirect(newUri);
    }
}